const errTypes = require('../constants/error-types');
const CustomError = require('../utils/error');

const validComment = (isReply) => {
  return async (ctx, next) => {
    const { content, momentId, commentId } = ctx.request.body;
    const mainOk = content && momentId;
    const replyOk = isReply && !commentId;
    if (!mainOk && !replyOk) {
      const err = new CustomError(errTypes.COMMENT_UNVALID);
      return ctx.app.emit('error', err, ctx);
    }
    await next();
  };
};

const validQuery = async (ctx, next) => {
  const { momentId } = ctx.query;
  if (!momentId) {
    const err = new CustomError(errTypes.MOMENTID_UNVALID);
    return ctx.app.emit('error', err, ctx);
  }
  await next();
};

module.exports = {
  validComment,
  validQuery,
};
